package net.ttny.java.web.action;

import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.qiniu.storage.Recorder;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.persistent.FileRecorder;
import com.qiniu.util.Auth;
import com.qiniu.common.Zone;
import com.qiniu.storage.Configuration;
//import com.qiniu.util.StringMap;
//import com.qiniu.storage.model.FileInfo;
//import com.qiniu.storage.BucketManager;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.codec.digest.DigestUtils;
import org.json.JSONArray;

public class QiniuOss {

    //设置好账号的ACCESS_KEY和SECRET_KEY
    String ACCESS_KEY = "ACCESS_KEY";
    String SECRET_KEY = "SECRET_KEY";
    //要上传的空间
    String bucketname = "bucketname";
    //上传到七牛后保存的文件名
    String key = getDate()+" "+getRand();
    //上传文件的路径
    String filePath = "d:/niyan/e1.png";

    //密钥配置
    Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY);

    ///////////////////////指定上传的Zone的信息//////////////////
    //第一种方式: 指定具体的要上传的zone
    //注：该具体指定的方式和以下自动识别的方式选择其一即可
    //要上传的空间(bucket)的存储区域为华东时
    // Zone z = Zone.zone0();
    //要上传的空间(bucket)的存储区域为华北时
     Zone z = Zone.zone1();
    //要上传的空间(bucket)的存储区域为华南时
    // Zone z = Zone.zone2();

    //第二种方式: 自动识别要上传的空间(bucket)的存储区域是华东、华北、华南。
    // Zone z = Zone.autoZone();
    Configuration c = new Configuration(z);

    //创建上传对象
    UploadManager uploadManager = new UploadManager(c);

    @SuppressWarnings("deprecation")
	String sign = DigestUtils.shaHex(key);

    public String getUpToken() {
        return auth.uploadToken(bucketname);
//    	return auth.uploadToken(bucketname, null, 3600, new StringMap()
//                .put("callbackUrl", "http://domain")
//                .put("callbackBody", "filename=$(fname)&filesize=$(fsize)"));
    }

    public void upload() throws IOException {
        //设置断点记录文件保存在指定文件夹或的File对象
        String recordPath = "d:/niyan/";
        //实例化recorder对象
        Recorder recorder = new FileRecorder(recordPath);
        //实例化上传对象，并且传入一个recorder对象
        UploadManager uploadManager = new UploadManager(c, recorder);

        try {
            //调用put方法上传
            Response res = uploadManager.put(filePath, "pj_ltc/tmp/"+getSha1(), getUpToken());
            //打印返回的信息
            System.out.println(res.bodyString());

            JSONArray jsonArray = null;
            jsonArray = new JSONArray("["+res.bodyString()+"]");
            // System.out.println(jsonArray.getJSONObject(0).get("hash"));
        } catch (QiniuException e) {
            Response r = e.response;
            //请求失败时打印的异常的信息
            System.out.println(r.toString());
            try {
                //响应的文本信息
                System.out.println(r.bodyString());
            } catch (QiniuException e1) {
                //ignore
            }
        }
    }

    public String getSha1() {
         return sign;
    }

    public static void main(String args[]) throws IOException {
        new QiniuOss().upload();
    }

    public String getDate() {
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(date);
    }

    public int getRand() {
        int rand = (int)(1+Math.random()*(998-100+1));
        return rand;
    }

}
